<script setup lang="ts">
import { onMounted } from 'vue'
import useCategoryStore from '@/store/modules/category'
const categoryStore = useCategoryStore()
//分类全局组件挂载完毕,通知仓库发请求获取一级分类的数据
onMounted(() => {
  getC1()
})
//通知仓库获取一级分类的方法
const getC1 = () => {
  //通知分类仓库发请求获取一级分类的数据
  categoryStore.getC1()
}
//一级分类下拉菜单的change事件(选中值的时候会触发,保证一级分类ID有了)
const handler = () => {
  //需要将二级、三级分类的数据清空
  categoryStore.c2Id = ''
  categoryStore.c3Arr = []
  categoryStore.c3Id = ''
  //通知仓库获取二级分类的数据
  categoryStore.getC2()
}
//二级分类下拉菜单的change事件(选中值的时候会触发,保证二级分类ID有了)
const handlers = () => {
  //清理三级分类的数据
  categoryStore.c3Id = ''
  categoryStore.getC3()
}
//接受父组件传递过来scene
defineProps(['scene'])
</script>
<template>
  <el-form label-width="auto" :inline="true">
    <el-form-item label="一级分类">
      <el-select
        :disabled="scene === 0 ? false : true"
        v-model="categoryStore.c1Id"
        @change="handler"
        placeholder="请选择"
        style="width: 150px"
      >
        <el-option
          v-for="item in categoryStore.c1Arr"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="二级分类">
      <el-select
        v-model="categoryStore.c2Id"
        :disabled="scene === 0 ? false : true"
        @change="handlers"
        placeholder="请选择"
        style="width: 150px"
      >
        <el-option
          v-for="item in categoryStore.c2Arr"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="三级分类">
      <el-select
        v-model="categoryStore.c3Id"
        :disabled="scene === 0 ? false : true"
        placeholder="请选择"
        style="min-width: 150px"
      >
        <el-option
          v-for="item in categoryStore.c3Arr"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
  </el-form>
</template>
<style scoped lang="scss"></style>
